home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / screen32.lha / screen-3.2b / putenv.c < prev    next >
C/C++ Source or Header  |  1992-02-02  |  6KB  |  203 lines

  1. /* Copyright (c) 1991
  2.  *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
  3.  *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
  4.  * Copyright (c) 1987 Oliver Laumann
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 1, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program (see the file COPYING); if not, write to the
  18.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * Noteworthy contributors to screen's design and implementation:
  21.  *    Wayne Davison (davison@borland.com)
  22.  *    Patrick Wolfe (pat@kai.com, kailand!pat)
  23.  *    Bart Schaefer (schaefer@cse.ogi.edu)
  24.  *    Nathan Glasser (nathan@brokaw.lcs.mit.edu)
  25.  *    Larry W. Virden (lwv27%cas.BITNET@CUNYVM.CUNY.Edu)
  26.  *    Howard Chu (hyc@hanauma.jpl.nasa.gov)
  27.  *    Tim MacKenzie (tym@dibbler.cs.monash.edu.au)
  28.  *    Markku Jarvinen (mta@{cc,cs,ee}.tut.fi)
  29.  *    Marc Boucher (marc@CAM.ORG)
  30.  *
  31.  ****************************************************************
  32.  */
  33.  
  34. /*
  35.  *  putenv  --  put value into environment
  36.  *
  37.  *  Usage:  i = putenv (string)
  38.  *    int i;
  39.  *    char  *string;
  40.  *
  41.  *  where string is of the form <name>=<value>.
  42.  *  If "value" is 0, then "name" will be deleted from the environment.
  43.  *  Putenv returns 0 normally, -1 on error (not enough core for malloc).
  44.  *
  45.  *  Putenv may need to add a new name into the environment, or to
  46.  *  associate a value longer than the current value with a particular
  47.  *  name.  So, to make life simpler, putenv() copies your entire
  48.  *  environment into the heap (i.e. malloc()) from the stack
  49.  *  (i.e. where it resides when your process is initiated) the first
  50.  *  time you call it.
  51.  *
  52.  *  HISTORY
  53.  *  3-Sep-91 Michael Schroeder (mlschroe). Modified to behave as
  54.  *    as putenv.
  55.  * 16-Aug-91 Tim MacKenzie (tym) at Monash University. Modified for
  56.  *    use in screen (iScreen) (ignores final int parameter)
  57.  * 14-Oct-85 Michael Mauldin (mlm) at Carnegie-Mellon University
  58.  *      Ripped out of CMU lib for Rob-O-Matic portability
  59.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  60.  *    Created for VAX.  Too bad Bell Labs didn't provide this.  It's
  61.  *    unfortunate that you have to copy the whole environment onto the
  62.  *    heap, but the bookkeeping-and-not-so-much-copying approach turns
  63.  *    out to be much hairier.  So, I decided to do the simple thing,
  64.  *    copying the entire environment onto the heap the first time you
  65.  *    call putenv(), then doing realloc() uniformly later on.
  66.  */
  67. #include "config.h"
  68.  
  69. #if defined(NEEDSETENV)
  70.  
  71. #define EXTRASIZE 5        /* increment to add to env. size */
  72.  
  73. char *index(), *malloc(), *realloc();
  74. int   strlen();
  75.  
  76. static int  envsize = -1;    /* current size of environment */
  77. extern char **environ;        /* the global which is your env. */
  78.  
  79. static int  findenv();        /* look for a name in the env. */
  80. static int  newenv();        /* copy env. from stack to heap */
  81. static int  moreenv();        /* incr. size of env. */
  82.  
  83. int unsetenv(name)
  84. char *name;
  85. {
  86.   register int i;
  87.   
  88.   i = findenv(name);
  89.   if (i<0)
  90.     return;            /* Already here */
  91.   
  92.   free(environ[i]);
  93.   if (envsize > 0)
  94.     envsize--;
  95.   for (; environ[i]; i++)
  96.     environ[i] = environ[i+1];
  97. }
  98.  
  99. int putenv(string)
  100. char *string;
  101.   register int  i, j;
  102.   register char *p;
  103.   
  104.   if (envsize < 0)
  105.     {                /* first time putenv called */
  106.       if (newenv() < 0)        /* copy env. to heap */
  107.     return (-1);
  108.     }
  109.   
  110.   i = findenv(string);        /* look for name in environment */
  111.  
  112.   if (i < 0)
  113.     {            /* name must be added */
  114.       for (i = 0; environ[i]; i++);
  115.       if (i >= (envsize - 1))
  116.     {            /* need new slot */
  117.       if (moreenv() < 0)
  118.         return (-1);
  119.     }
  120.       p = malloc(strlen(string) + 1);
  121.       if (p == 0)        /* not enough core */
  122.     return (-1);
  123.       environ[i + 1] = 0;    /* new end of env. */
  124.     }
  125.   else
  126.     {            /* name already in env. */
  127.       p = realloc(environ[i], strlen(string) + 1);
  128.       if (p == 0)
  129.     return (-1);
  130.     }
  131.   sprintf(p, "%s", string); /* copy into env. */
  132.   environ[i] = p;
  133.   
  134.   return (0);
  135. }
  136.  
  137. static int  findenv(name)
  138. char *name;
  139. {
  140.   register char *namechar, *envchar;
  141.   register int  i, found;
  142.   
  143.   found = 0;
  144.   for (i = 0; environ[i] && !found; i++)
  145.     { 
  146.       envchar = environ[i];
  147.       namechar = name;
  148.       while (*namechar && *namechar != '=' && (*namechar == *envchar))
  149.         { 
  150.       namechar++;
  151.       envchar++;
  152.         }
  153.       found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
  154.     }
  155.   return (found ? i - 1 : -1);
  156. }
  157.  
  158. static int newenv()
  159.   register char **env, *elem;
  160.   register int i, esize;
  161.  
  162.   for (i = 0; environ[i]; i++)
  163.     ;
  164.   esize = i + EXTRASIZE + 1;
  165.   env = (char **)malloc(esize * sizeof (elem));
  166.   if (env == 0)
  167.     return (-1);
  168.  
  169.   for (i = 0; environ[i]; i++)
  170.     { 
  171.       elem = malloc(strlen(environ[i]) + 1);
  172.       if (elem == 0)
  173.     return (-1);
  174.       env[i] = elem;
  175.       strcpy(elem, environ[i]);
  176.     }
  177.    
  178.   env[i] = 0;
  179.   environ = env;
  180.   envsize = esize;
  181.   return (0);
  182. }
  183.  
  184. static int moreenv()
  185.   register int  esize;
  186.   register char **env;
  187.   
  188.   esize = envsize + EXTRASIZE;
  189.   env = (char **)realloc(environ, esize * sizeof (*env));
  190.   if (env == 0)
  191.     return (-1);
  192.   environ = env;
  193.   envsize = esize;
  194.   return (0);
  195. }
  196.  
  197. #endif /* NEEDSETENV */
  198.  
  199.  
  200.